package com.geekaca.testthread.wokr2;

import java.util.concurrent.locks.ReentrantLock;

public class TeestReentrantLock {
    public static void main(String[] args) {

        Station station = new Station();
        Thread thread1 = new Thread(station);
        Thread thread2 = new Thread(station);
        thread1.setName("booth1");
        thread2.setName("booth2");

        thread1.start();
        thread2.start();
    }

}

class Station implements Runnable {
    int ticket = 1;
    private ReentrantLock rLock = new ReentrantLock();

    @Override
    public void run() {
        while (ticket <=20) {
            rLock.lock();
            try{
                System.out.println(Thread.currentThread().getName() + " 在卖第" + ticket + "票...");
                ticket++;
            }finally {
                rLock.unlock();
            }

        }
    }
}

